package com.nd.mobile.study.domain;

import java.io.Serializable;
import java.lang.reflect.Type;

import android.os.Bundle;

import com.fuckhtc.gson.Gson;
import com.fuckhtc.gson.annotations.SerializedName;
import com.fuckhtc.gson.reflect.TypeToken;
import com.nd.mobile.study.common.config.Commands;
import com.nd.mobile.study.common.config.ReqParam;
import com.nd.mobile.study.common.util.HttpRequestUtil;

public class NewsCatalogRoot implements Serializable{

	private static final long	serialVersionUID	= -8627510390343353660L;

	public static final int DEFAULT_NEWS_CATALOG_ROOT_ID = 66;

	public static final int	DEFAULT_NEWS_CATALOG_PAGE_SIZE	= 5;
	
	@SerializedName("InfoClassID")
	private String id;
	
	@SerializedName("InfoClassName")
	private String title;
	
	@SerializedName("PicUrl")
	private String picURL;
	
	@SerializedName("NO")
	private String orderNO;
	
	public String getTitle() {
		return title;
	}

	public String getPicURL() {
		return picURL;
	}

	public static Result<NewsCatalogRoot[]> loadFromRemote() throws Exception {
		Bundle data = new Bundle();
		data.putInt(ReqParam.NEWS_CATALOG_ROOT_ID, DEFAULT_NEWS_CATALOG_ROOT_ID);
		String jsonData = HttpRequestUtil.get(Commands.GET_NEWS_CATALOG_ROOT, data);
		Gson gson = new Gson();
		Type type = new TypeToken<Result<NewsCatalogRoot[]>>(){}.getType();
		Result<NewsCatalogRoot[]> newsCatalogRoot = gson.fromJson(jsonData, type);
		return newsCatalogRoot;
	}

	public Result<News[]> getNews() throws Exception {
		Bundle data = new Bundle();
		data.putString(ReqParam.NEWS_CATALOG_ID, id);
		data.putInt(ReqParam.NEWS_CATALOG_PAGE_SIZE, DEFAULT_NEWS_CATALOG_PAGE_SIZE);
		String jsonData = HttpRequestUtil.get(Commands.GET_NEWS_CATALOG, data);
		Gson gson = new Gson();
		Type type = new TypeToken<Result<News[]>>(){}.getType();
		Result<News[]> list = gson.fromJson(jsonData, type);
		return list;
	}
	
	public static News[] getLatestNews() throws Exception {
		Bundle data = new Bundle();
		data.putInt(ReqParam.NEWS_CATALOG_ID, DEFAULT_NEWS_CATALOG_ROOT_ID);
		data.putInt(ReqParam.NEWS_CATALOG_PAGE_SIZE, DEFAULT_NEWS_CATALOG_PAGE_SIZE);
		String jsonData = HttpRequestUtil.get(Commands.GET_LATEST_NEWS_DETAIL, data);
		Gson gson = new Gson();
		Type type = new TypeToken<Result<News[]>>(){}.getType();
		Result<News[]> list = gson.fromJson(jsonData, type);
		return list.getBody();
	}
	
}
